#include<iostream>
#include<fstream>
#include<string>
#include <vector>
#include <iterator>
#include <sstream>
using namespace std;
string getNextToken1(istream &in);
vector<string> pairwords() {

    vector<string> words;
    ifstream fin("wordpair_stemmed.txt");
    string s1;
    string empty ="";
    while((s1=getNextToken1(fin))!=empty )
           {
             //  cout<<s1;
            words.push_back (s1);
           }
    return words;
     }

string getNextToken1(istream &in)
{
    char c;
    string ans="";
    c=in.get();
    while(!isalpha(c) && !in.eof())//cleaning non letter charachters
    {

        c=in.get();
    }
    while(isalpha(c))
    {
        ans.push_back(tolower(c));
        c=in.get();
    }
    return ans;
}

string getNextToken(stringstream &in)
{
    //stringstream in;
    //in << str1;
    char c;
    string ans="";
    c=in.get();
    while(!isalpha(c) && !in.eof())//cleaning non letter charachters
    {

        c=in.get();
    }
    while(isalpha(c))
    {
        ans.push_back(tolower(c));
        c=in.get();
    }
    return ans;
}


int main(int argc, char * argv[]) {
    ifstream myReadFile;
    myReadFile.open(argv[1]);
    string line;
    vector<string> store;
    if (myReadFile.is_open()) {
    while (!myReadFile.eof()) {
       // cout<<1<<endl;
    getline (myReadFile, line);
    store.push_back(line);
}}
    myReadFile.close();

    vector<string>wordpair;
    wordpair=pairwords();

    string s;
    int flag;
    string empty ="";
    vector<string>::iterator it, iter;
    for(it=store.begin(); it != store.end(); it++)
    {
        //cout<<2<<endl;

    for(iter=wordpair.begin(); iter != wordpair.end(); iter++)
                {
                    flag=0;
                    stringstream input;
                    input<<*it;
                    while((s=getNextToken(input))!=empty )
                    {
                    //cout<<s<<endl;
                    if (*iter==s && flag ==0)
                    flag=1;
                    if (*(iter+1)==s && flag==0)
                    flag=2;
                    if (*iter==s && flag ==2)
                    flag=3;
                    if (*(iter+1)==s && flag==1)
                    flag=3;
                }
                if(flag==3)
                {
                    cout<<*iter<<*(iter+1)<<" ";
                    string fname="C:\\Users\\Rishi\\Documents\\BTP\\"+*iter+*(iter+1)+".txt";
                    ofstream fout;
                    fout.open((fname).c_str(),ios::app);
                    //fout<<"WORD PAIR "<<*iter<<" - "<<*(iter+1)<<endl;
                    fout<<*it<<endl;
                }
                iter++;

            }
    }
}
